//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Imaging;
using DNAide.Core;

namespace DNAide.Drawing
{

	public static partial class BitmapAide
	{

		/// <summary>
		/// Resizes a bitmap
		/// </summary>
		/// <param name="bitmapToResize"></param>
		/// <param name="newWidth"></param>
		/// <param name="newHeight"></param>
		public static void Resize(ref Bitmap bitmapToResize, Int32 newWidth, Int32 newHeight)
		{
			Resize(ref bitmapToResize, newWidth, newHeight, true);
		}


		/// <summary>
		/// Resizes a bitmap
		/// </summary>
		/// <param name="bitmapToResize"></param>
		/// <param name="newWidth"></param>
		/// <param name="newHeight"></param>
		/// <param name="maintainAspectRatio"></param>
		public static void Resize(ref Bitmap bitmapToResize, Int32 newWidth, Int32 newHeight, Boolean maintainAspectRatio)
		{
			Resize(ref bitmapToResize, newWidth, newHeight, maintainAspectRatio, new GraphicsSettings());
		}

		
		/// <summary>
		/// Resizes a bitmap
		/// </summary>
		/// <param name="bitmapToResize"></param>
		/// <param name="newWidth"></param>
		/// <param name="newHeight"></param>
		/// <param name="maintainAspectRatio"></param>
		/// <param name="graphicsSettings"></param>
		public static void Resize(ref Bitmap bitmapToResize, Int32 newWidth, Int32 newHeight, Boolean maintainAspectRatio, GraphicsSettings graphicsSettings)
		{
			int calculatedNewWidth = newWidth;
			int calculatedNewHeight = newHeight;
			if (calculatedNewWidth < 1)
			{
				calculatedNewWidth = bitmapToResize.Width;
			}
			if (calculatedNewHeight < 1)
			{
				calculatedNewHeight = bitmapToResize.Height;
			}
			int originalWidth = bitmapToResize.Width;
			int originalHeight = bitmapToResize.Height;

			if (maintainAspectRatio)
			{
				double ratio, final;

				if (originalWidth > originalHeight)
				{
					ratio = (double)((double)(originalHeight) / (double)(originalWidth));
					final = (calculatedNewWidth) * ratio;
					calculatedNewHeight = (int)final;

					if (newHeight > 0 && calculatedNewHeight > newHeight)
					{
						// The calculated height exceeds newHeight, so recalcuate
						ratio = (double)((double)(newHeight) / (double)(calculatedNewHeight));
						final = (calculatedNewWidth) * ratio;
						calculatedNewWidth = (int)final;
						calculatedNewHeight = newHeight;
					}
				}
				else
				{
					ratio = (double)((double)(originalWidth) / (double)(originalHeight));
					final = (calculatedNewHeight) * ratio;
					calculatedNewWidth = (int)final;

					if (newWidth > 0 && calculatedNewWidth > newWidth)
					{
						// The calculated width exceeds newWidth, so recalculate
						ratio = (double)((double)(newWidth) / (double)(calculatedNewWidth));
						final = (calculatedNewHeight) * ratio;
						calculatedNewHeight = (int)final;
						calculatedNewWidth = newWidth;
					}
				}
			}

			Bitmap resizedBitmap = new Bitmap(calculatedNewWidth, calculatedNewHeight);
			using (Graphics g = Graphics.FromImage((Image)resizedBitmap))
			{
				if (graphicsSettings != null)
				{
					graphicsSettings.ApplySettings(g);
				}
				g.DrawImage(bitmapToResize, 0, 0, calculatedNewWidth, calculatedNewHeight);
			}
			bitmapToResize.Dispose();	bitmapToResize = null;
			bitmapToResize = BitmapAide.CopyBitmap(resizedBitmap);
			resizedBitmap.Dispose(); resizedBitmap = null;
		}

	}
}